package com.qiangshi.common.utils;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PageInfoConvent
 * @Description
 * @Author braless
 * @Date 2021/8/29 18:17
 */
public class PageInfoConvent<T> {
    private int pageNum;
    private int pageSize;
    private int startRow;
    private int endRow;
    private int pages;
    private List<T> list= new ArrayList<T>() ;
    public PageInfoConvent(int pageNum, int pageSize, List<T> totalList) {
        this.pageNum = pageNum<=1 ? 1:pageNum;
        this.pageSize = pageSize<=1 ? 1:pageSize;
        if (pageSize>totalList.size()) {
            this.pageSize = totalList.size();
        }
        if(CollectionUtils.isEmpty(totalList)){
            return ;
        }
        int totalCount = totalList.size();
        if(totalCount<=pageSize){
            this.pages=1;
        }else{
            if(totalCount%pageSize>0){
                this.pages = totalCount/pageSize+1;
            }else{
                this.pages = totalCount/pageSize;
            }
        }
        this.startRow = this.pageNum > 0 ? (this.pageNum - 1) * this.pageSize : 0;
        this.endRow = this.startRow + this.pageSize * (this.pageNum > 0 ? 1 : 0);
        if(endRow>totalCount)endRow=totalCount;
        list = totalList.subList(this.startRow,this.endRow);
    }
//getter /setter 方法

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getStartRow() {
        return startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public int getEndRow() {
        return endRow;
    }

    public void setEndRow(int endRow) {
        this.endRow = endRow;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}